package com.molyfun.parents.activity.myorder.view;

import android.content.Context;
import android.support.v4.app.FragmentManager;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.molyfun.parents.R;
import com.molyfun.parents.activity.coursecenter.model.SubjectListResponse;
import com.molyfun.parents.activity.myorder.OrderAdapter;
import com.molyfun.parents.fragment.dialog.CommonChooseDialogFragment;
import com.molyfun.parents.fragment.dialog.RefundDetailDialogFragment;
import com.molyfun.parents.rest.bean.OrderDetailResponse;
import com.molyfun.parents.utils.TimeUtils;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by william(liuchao.hit@gmail.com).
 * Date:10/18/16.
 */
public class OrderView extends LinearLayout implements View.OnClickListener {

    protected LayoutInflater layoutInflater;

    private static final String mPayStr = "支付";
    private static final String mRefundStr = "退款";
    private static final String mRefundingStr = "退款中";
    private static final String mRefundDetailStr = "查看详情";
    private static final String mRefundReasonStr = "查看原因";

    private Context context;

    @InjectView(R.id.order_time)
    TextView mOrderTime;
    @InjectView(R.id.order_state)
    ImageView mOrderState;
    @InjectView(R.id.product_name)
    TextView mProductName;
    @InjectView(R.id.price_now)
    TextView mProductPriceNow;
    @InjectView(R.id.order_pay_now)
    TextView mOrderPay;
//    @InjectView(R.id.order_no)
//    TextView mOrderNo;
    //    @InjectView(R.id.order_cancel)
//    TextView mOrderCancel;
    @InjectView(R.id.order_del)
    ImageView mOrderDel;
    @InjectView(R.id.order_count_down_layout)
    LinearLayout mOrderCountDownLayout;
    @InjectView(R.id.second_last)
    TextView mSecondLast;
    @InjectView(R.id.line)
    View mLine;
    @InjectView(R.id.class_type)
    ImageView mClassType;

    private OrderDetailResponse.OrderBean mOrderBean;

    private FragmentManager mFragmentManager;

    public OrderView(Context context, AttributeSet attrs, int defStyle) {
        this(context, attrs);
    }

    public OrderView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public OrderView(Context context) {
        super(context);
        init(context, null);
    }

    private void init(Context context, AttributeSet attrs) {
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
        layoutInflater.inflate(R.layout.widget_order, this);
        ButterKnife.inject(this);
        mOrderPay.setOnClickListener(this);
//        mOrderCancel.setOnClickListener(this);
        mOrderDel.setOnClickListener(this);
    }

    private OrderAdapter.OnOrderOutOfDateCallback mOnOrderOutOfDateCallback;

    public void setOnOrderOutOfDateCallback(OrderAdapter.OnOrderOutOfDateCallback onOrderOutOfDateCallback) {
        this.mOnOrderOutOfDateCallback = onOrderOutOfDateCallback;
    }

    public void initData(OrderDetailResponse.OrderBean orderBean, FragmentManager fragmentManager) {
        initData(orderBean, fragmentManager, true);
    }

    /**
     * init view
     */
    public void initData(OrderDetailResponse.OrderBean orderBean, FragmentManager fragmentManager, boolean showLine) {
        mFragmentManager = fragmentManager;
        if (orderBean != null) {
            mOrderBean = orderBean;

            mOrderTime.setText("下单时间: " + TimeUtils.getDateByTimeMillis(orderBean.getCreatedatetime()));
//            mOrderNo.setText("订单编号: " + orderBean.getOuttradeno());
            if (orderBean.getProductList() != null && orderBean.getProductList().size() > 0) {
                mProductName.setText(orderBean.getProductList().get(0).getName());
                mProductPriceNow.setText("￥" + orderBean.getProductList().get(0).getTotal());
            } else if (orderBean.getSubject() != null) {
                SubjectListResponse.SubjectBrief subjectBrief = orderBean.getSubject();
                mProductName.setText(subjectBrief.getName());
                mProductPriceNow.setText("￥" + subjectBrief.getPrice());
            }else if(orderBean.getGroup() != null){
                mProductName.setText(orderBean.getGroup().getName());
                mProductPriceNow.setText("￥" + orderBean.getAmount());
            }else{
                mProductName.setText("无套餐标题");
                mProductPriceNow.setText("￥" + orderBean.getAmount());
            }

            //init 待付款 pass 已付款 refundapply 退款申请 refund 已退款
            if (TextUtils.isEmpty(orderBean.getStatus()) || !orderBean.getStatus().equals("init")) {
                mOrderState.setImageResource(R.mipmap.icon_order_paid);
                mOrderCountDownLayout.setVisibility(View.GONE);
                mOrderPay.setVisibility(View.GONE);

                if (!TextUtils.isEmpty(orderBean.getStatus())) {
                    if (orderBean.getStatus().equals("pass")) {
                        mOrderCountDownLayout.setVisibility(View.GONE);
                        if (!TextUtils.isEmpty(orderBean.getAllowrefundstatus()) && orderBean.getAllowrefundstatus().equals("y")) {
                            mOrderPay.setVisibility(View.VISIBLE);
                            mOrderPay.setText(mRefundStr);
                        } else {

                            mOrderPay.setVisibility(View.GONE);
                        }
                    } else if (orderBean.getStatus().equals("refundapply")) {
                        if (orderBean.getOrderrefunds() != null) {
                            //拒绝原因为空
                            if (TextUtils.isEmpty(orderBean.getOrderrefunds().getRejectreason())) {
                                mOrderPay.setVisibility(View.INVISIBLE);
                                mOrderCountDownLayout.setVisibility(View.VISIBLE);
                                mSecondLast.setTextColor(ContextCompat.getColor(context, R.color.color_base));
                                mSecondLast.setText(mRefundingStr);
                            } else {
                                mOrderPay.setVisibility(View.VISIBLE);
                                mOrderPay.setText(mRefundReasonStr);
                                mOrderCountDownLayout.setVisibility(View.VISIBLE);
                                mSecondLast.setTextColor(ContextCompat.getColor(context, R.color.color_pay_text));
                                mSecondLast.setText("退款失败");
                            }

                        }
                    }else if (orderBean.getStatus().equals("refund")) {
                        if (orderBean.getOrderrefunds() != null) {
                            mOrderPay.setVisibility(View.VISIBLE);
                            mOrderPay.setText(mRefundDetailStr);
                            mOrderCountDownLayout.setVisibility(View.VISIBLE);
                            mSecondLast.setTextColor(ContextCompat.getColor(context, R.color.color_base));
                            mSecondLast.setText("已退款" + orderBean.getOrderrefunds().getAmount() + "元");
                        }
                    }

                }

            } else {
                mOrderPay.setVisibility(View.VISIBLE);
                mOrderPay.setText(mPayStr);
                mOrderState.setImageResource(R.mipmap.icon_order_unpaid);
                if (orderBean.getExpiredatetime() != 0l) {
                    long timeNow = System.currentTimeMillis();
                    if (timeNow >= orderBean.getExpiredatetime()) {
                        //TODO 超过时间了，删除
                        if (mOnOrderOutOfDateCallback != null) {
                            mOnOrderOutOfDateCallback.onOrderOutOfDate(orderBean);
                        }
                    } else {
                        mOrderCountDownLayout.setVisibility(View.VISIBLE);
                        mSecondLast.setTextColor(ContextCompat.getColor(context, R.color.color_pay_text));
                        mSecondLast.setText("订单将在" + TimeUtils.getCountDownByTimeMillis(orderBean.getExpiredatetime()) + "后失效");
                    }
                } else {
                    mOrderCountDownLayout.setVisibility(View.GONE);
                }
            }

            if (!TextUtils.isEmpty(orderBean.getType())) {
                if (orderBean.getType().equals("subject")) {
                    mClassType.setVisibility(View.VISIBLE);
                    mClassType.setImageResource(R.mipmap.ic_pay_record_open_class);
                } else if (orderBean.getType().equals("group")) {
                    mClassType.setVisibility(View.VISIBLE);
                    mClassType.setImageResource(R.mipmap.ic_pay_record_class);
                } else {
                    mClassType.setVisibility(View.GONE);
                }
            } else {
                mClassType.setVisibility(View.GONE);
            }

        }
        if (showLine) {
            mLine.setVisibility(View.VISIBLE);
        } else {
            mLine.setVisibility(View.GONE);
        }
    }

    private OrderViewCallback mOrderViewCallback;

    public void setOrderViewCallback(OrderViewCallback orderViewCallback) {
        this.mOrderViewCallback = orderViewCallback;
    }

    public interface OrderViewCallback {
        void onOrderPay(OrderDetailResponse.OrderBean orderBean);

        void onOrderCancel(String orderId);

        void onOrderDel(String orderId);

        void onRefound(String orderId);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.order_del:
                if (mOrderViewCallback != null) {
                    if (TextUtils.isEmpty(mOrderBean.getStatus()) || !mOrderBean.getStatus().equals("init")) {
                        new CommonChooseDialogFragment.CommonChooseDialogBuilder("您确定删除订单吗？")
                                .setCommonChooseDialogClickListener(new CommonChooseDialogFragment.CommonChooseDialogClickListener() {
                                    @Override
                                    public void onConfirm() {
                                        mOrderViewCallback.onOrderDel(mOrderBean.getId());
                                    }

                                    @Override
                                    public void onCancel() {

                                    }
                                })
                                .build(mFragmentManager.beginTransaction());
                    } else {
                        new CommonChooseDialogFragment.CommonChooseDialogBuilder("您确定要取消订单吗？")
                                .setCommonChooseDialogClickListener(new CommonChooseDialogFragment.CommonChooseDialogClickListener() {
                                    @Override
                                    public void onConfirm() {
                                        mOrderViewCallback.onOrderCancel(mOrderBean.getId());
                                    }

                                    @Override
                                    public void onCancel() {

                                    }
                                })
                                .build(mFragmentManager.beginTransaction());
                    }
                }
                break;
            case R.id.order_pay_now:
                //TODO 立即支付功能
                switch (mOrderPay.getText().toString().trim()) {
                    case mRefundStr:
                        if (mOrderViewCallback != null) {
                            mOrderViewCallback.onRefound(mOrderBean.getId());
                        }
                        break;
                    case mRefundDetailStr:
                        //退款详情
                        new RefundDetailDialogFragment.RefundDetailDialogBuilder(mOrderBean.getOrderrefunds().getAmount() + "", mOrderBean.getOrderrefunds().getRefundreason())
                                .build(mFragmentManager.beginTransaction());
                        break;
                    case mPayStr:
                        if (mOrderViewCallback != null) {
                            mOrderViewCallback.onOrderPay(mOrderBean);
                        }
                        break;

                    case mRefundReasonStr:
                        //查看原因
                        new CommonChooseDialogFragment.CommonChooseDialogBuilder("取消", "确定", mOrderBean.getOrderrefunds().getRejectreason() + "(是否继续退款)", "退款失败原因")
                                .setCommonChooseDialogClickListener(new CommonChooseDialogFragment.CommonChooseDialogClickListener() {
                                    @Override
                                    public void onConfirm() {
                                        if(mOrderViewCallback != null){
                                            mOrderViewCallback.onRefound(mOrderBean.getId());
                                        }
                                    }

                                    @Override
                                    public void onCancel() {
                                    }
                                })
                                .build(mFragmentManager.beginTransaction());
                        break;
                }

                break;
        }
    }

}